package com.zd.mole.site.mohurd.process;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.zd.mole.process.ProcessHandler;
import com.zd.mole.site.mohurd.entity.Ot_tenders_info;
import com.zd.mole.site.mohurd.repository.OtCompanyInfoRepository;
import com.zd.mole.site.mohurd.repository.OtPersonInfoRepository;
import com.zd.mole.site.mohurd.service.SysDictService;
import com.zd.mole.task.TaskStatus;
import com.zd.mole.task.entity.Task;
import com.zd.mole.task.service.TaskService;
import com.zd.mole.utils.RegexUtils;

/**
 * 项目数据/招投标
 * @author ZhangDi
 *
 */
@Component
@Transactional
public class MohurdDataserviceQueryProjectTenderInfoHandler implements ProcessHandler {

	private Logger log = LoggerFactory.getLogger(getClass());
	
	@PersistenceContext 
	private EntityManager em;
	@Resource
	private SysDictService sysDictService;
	@Resource
	private OtCompanyInfoRepository otCompanyInfoRepository;
	@Resource
	private OtPersonInfoRepository otPersonInfoRepository;
	@Resource
	private TaskService taskService;
	
	@Override
	public List<Task> handler(Task task, String text) {
		
		Ot_tenders_info t = new Ot_tenders_info();
		String id = task.getRequestUrl().replaceFirst("/dataservice/query/project/tenderInfo/", "");
		t.setId(id);
		t.setProjectNo(task.getCode());
		t.setWinNotifyTender(RegexUtils.find("<td data-header=\"中标通知书编号\">" + INNER_HTML + "</td>", text));
		t.setProwinNotifyTender(RegexUtils.find("<td data-header=\"省级中标通知书编号\">" + INNER_HTML + "</td>", text));
		String TenderType = RegexUtils.find("<td data-header=\"招标类型\">" + INNER_HTML + "</td>", text);
		TenderType = sysDictService.findOrSaveByTypeLabel("oLife_ProTenderType", TenderType, "招标类型");
		t.setTenderType(TenderType);
		String TenderWay = RegexUtils.find("<td data-header=\"招标方式\">" + INNER_HTML + "</td>", text);
		TenderWay = sysDictService.findOrSaveByTypeLabel("oLife_ProTenderWay", TenderWay, "招标方式");
		t.setTenderWay(TenderWay);
		t.setWinSum(RegexUtils.find("<td data-header=\"中标金额（万元）\">" + INNER_HTML + "</td>", text));
		t.setWinDate(RegexUtils.find("<td data-header=\"中标日期\">" + INNER_HTML + "</td>", text));
		t.setDevlopScope(RegexUtils.find("<td colspan=\"3\" data-header=\"建设规模\">" + INNER_HTML + "</td>", text));
		t.setTotalArea(RegexUtils.find("<td colspan=\"3\" data-header=\"面积（平方米）\">" + INNER_HTML + "</td>", text));
		String proxyUnitName = RegexUtils.find("<td data-header=\"招标代理单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		if("".equals(proxyUnitName)) {
			proxyUnitName = RegexUtils.find("<td data-header=\"招标代理单位名称\">\\s*" + INNER_HTML + "\\s*</td>", text);
		}
		t.setProxyUnitName(proxyUnitName);
		t.setProxyUnitCode(RegexUtils.find("<td data-header=\"招标代理单位组织机构代码\">" + INNER_HTML + "</td>", text));
		String proxyUnitCompanyId = RegexUtils.find("<td data-header=\"招标代理单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">", text);
		t.setProxyUnitCompanyId(proxyUnitCompanyId);
		if(!"".equals(proxyUnitCompanyId) && !otCompanyInfoRepository.existsById(proxyUnitCompanyId)) {
			Task nt = new Task();
			nt.setCode(proxyUnitCompanyId);
			nt.setHostUrl(task.getHostUrl());
			nt.setRequestUrl("/dataservice/query/comp/compDetail/" + proxyUnitCompanyId);
			nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
			nt.setStatus(TaskStatus.Ready);
			taskService.save(nt);
		}
		String winUnitName= RegexUtils.find("<td data-header=\"中标单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>\\s*", text);
		if("".equals(winUnitName)) {
			winUnitName = RegexUtils.find("<td data-header=\"中标单位名称\">\\s*" + INNER_HTML + "\\s*</td>", text);
		}
		t.setWinUnitName(winUnitName);
		t.setWinUnitCode(RegexUtils.find("<td data-header=\"中标单位组织机构代码\">" + INNER_HTML + "</td>", text));
		String winUnitCompanyId = RegexUtils.find("<td data-header=\"中标单位名称\">\\s*<a href=\"/dataservice/query/comp/compDetail/(\\d+)\" target=\"_blank\">", text);
		t.setWinUnitCompanyId(winUnitCompanyId);
		if(!"".equals(winUnitCompanyId) && !otCompanyInfoRepository.existsById(proxyUnitCompanyId)) {
			Task nt = new Task();
			nt.setCode(winUnitCompanyId);
			nt.setHostUrl(task.getHostUrl());
			nt.setRequestUrl("/dataservice/query/comp/compDetail/" + winUnitCompanyId);
			nt.setProcessHandlerClassName("MohurdDataserviceQueryCompCompDetailHandler");
			nt.setStatus(TaskStatus.Ready);
			taskService.save(nt);
		}
		String unitManagerPersionId = RegexUtils.find("<td data-header=\"项目经理/总监理工程师姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/(\\d+)\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text);
		t.setUnitManagerPersionId(unitManagerPersionId);
		if(!"".equals(unitManagerPersionId)) {
			t.setUnitManagerName(RegexUtils.find("<td data-header=\"项目经理/总监理工程师姓名\">\\s*<a href=\"/dataservice/query/staff/staffDetail/\\d+\" target=\"_blank\">" + INNER_HTML + "</a>\\s*</td>", text));
			if(!otPersonInfoRepository.existsById(unitManagerPersionId)) {
				Task nt = new Task();
				nt.setCode(unitManagerPersionId);
				nt.setHostUrl(task.getHostUrl());
				nt.setRequestUrl("/dataservice/query/staff/staffDetail/" + unitManagerPersionId);
				nt.setProcessHandlerClassName("MohurdDataserviceQueryStaffstaffDetailHandler");
				nt.setStatus(TaskStatus.Ready);
				taskService.save(nt);
			}
		} else { 
			t.setUnitManagerName(RegexUtils.find("<td data-header=\"项目经理/总监理工程师姓名\">\\s*" + INNER_HTML + "\\s*</td>", text));
		}
		
		t.setProjectManagerCode(RegexUtils.find("<td data-header=\"项目经理/总监理工程师身份证号码\">\\s*" + INNER_HTML + "\\s*</td>", text));
		String recordDate = RegexUtils.find("<td colspan=\"3\" data-header=\"记录登记时间\">" + INNER_HTML + " </td>", text);
		if(!"1900-01-01".equals(recordDate) && recordDate.length() == 10) {
			try {
				t.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(recordDate));
			} catch (ParseException e) {
				log.warn("日期转换失败 {}", e.getMessage());
			}
		}
		t.setCreate_by("1");
		t.setCreate_date(new Date());
		t.setUpdate_by("1");
		t.setUpdate_date(new Date());
		t.setDel_flag("0");
		em.merge(t);
		return null;
	}

}
